package persistent

import (
	"errors"
	"gitee.com/CrazyMage/lite-note/internal/entity"
	"gorm.io/gorm"
)

type UserRepo struct {
	*gorm.DB
}

func NewUserRepo(db *gorm.DB) *UserRepo {
	return &UserRepo{db}
}

func (ur *UserRepo) CreateUser(user *entity.User) error {

	if user == nil || user.UserName == "" || user.PasswordDigest == "" {
		return errors.New("user is nil or empty in user_mysql")
	}

	return ur.DB.Create(user).Error

}

func (ur *UserRepo) FindUserByUserName(username string) (user *entity.User, err error) {

	user = &entity.User{}
	err = ur.DB.Where("user_name =?", username).First(user).Error
	return
}
